package com.bijiawang.search.controller;

import com.bijiawang.search.dao.Srot;
import com.bijiawang.search.pojo.Goods;
import com.bijiawang.search.service.SearchOtherService;
import com.bijiawang.search.service.SearchTBSNService;
import com.sun.istack.internal.NotNull;
import com.sun.org.glassfish.gmbal.ParameterNames;
import jdk.nashorn.internal.scripts.JD;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Controller
public class SearchController {

    private int yeshu = 1;

    @Autowired
    SearchOtherService searchOtherService;

    @Autowired
    SearchTBSNService searchTBSNService;

    @GetMapping("/search")
    public String search(@RequestParam("q") String key, Model model, @RequestParam("sort") String sort,
                         @RequestParam("platform") String platform,@RequestParam(value = "PageName" ,defaultValue = "1")int pagename) throws InterruptedException, IOException, ExecutionException {
         ArrayList<Goods> TaoBaoList = null;
        ArrayList<Goods> SuNingList = null;
        ArrayList<Goods> TmallList = null;
        ArrayList<Goods> DDList = null;
        ArrayList<Goods> JDList = null;
        ArrayList<Goods> List = null;
        ArrayList<Goods> goodsArrayList = new ArrayList<Goods>();

        if (platform.equals("All")) {
            Future<ArrayList<Goods>> TaoBao = searchTBSNService.SearchTaobao(key);
            Future<ArrayList<Goods>> SuNing = searchTBSNService.SearchSuNing(key);
            Future<ArrayList<Goods>> Tmall = searchOtherService.SearchTmall(key);
            Future<ArrayList<Goods>> DD = searchOtherService.SearchDD(key);
            Future<ArrayList<Goods>> JD = searchOtherService.SearchJD(key);
            TaoBaoList = TaoBao.get();
            SuNingList = SuNing.get();
            TmallList = Tmall.get();
            DDList = DD.get();
            JDList = JD.get();
        }else {
            if (platform.contains("Taobao")) {
                Future<ArrayList<Goods>> TaoBao = searchTBSNService.SearchTaobao(key);
                TaoBaoList = TaoBao.get();
            }
            if (platform.contains("SuNing")) {
                Future<ArrayList<Goods>> SuNing = searchTBSNService.SearchSuNing(key);
                SuNingList = SuNing.get();
            }
            if (platform.contains("Tmall")) {
                Future<ArrayList<Goods>> Tmall = searchOtherService.SearchTmall(key);
                TmallList = Tmall.get();
            }
            if (platform.contains("JD")) {
                Future<ArrayList<Goods>> JD = searchOtherService.SearchJD(key);
                JDList = JD.get();
            }
            if (platform.contains("DD")) {
                Future<ArrayList<Goods>> DD = searchOtherService.SearchDD(key);
                DDList = DD.get();
            }
        }

        if (TaoBaoList != null){
            goodsArrayList.addAll(TaoBaoList);
        }
        if (SuNingList != null){
            goodsArrayList.addAll(SuNingList);
        }
        if (TmallList != null){
            goodsArrayList.addAll(TmallList);
        }
        if (DDList != null) {
            goodsArrayList.addAll(DDList);
        }
        if (JDList != null){
            goodsArrayList.addAll(JDList);
        }

        if (sort == null || sort.equals("top")){
            List = Srot.QuickSort(goodsArrayList, 0, goodsArrayList.size() - 1);
        }else {
            List = Srot.buttomSort(goodsArrayList, 0, goodsArrayList.size() - 1);
        }

        if (platform.equals("All")){
            platform = "Taobao,JD,DD,Tmall,SuNing";
        }

        int number;

        if (List != null){
            number = (int)Math.ceil(List.size() / 40.0);
        }else{
            number = 0;
        }

        model.addAttribute("pagename",pagename);
        model.addAttribute("number",number);
        model.addAttribute("platform",platform);
        model.addAttribute("sort",sort);
        model.addAttribute("List",List);
        model.addAttribute("key",key);
        return "search";
    }


    @ResponseBody
    @GetMapping("/testsearch")
    public ArrayList<Goods> testsearch(@RequestParam("q") String key, @RequestParam("sort") String sort,
                         @RequestParam("platform") String platform) throws InterruptedException, IOException, ExecutionException {
        ArrayList<Goods> TaoBaoList = null;
        ArrayList<Goods> SuNingList = null;
        ArrayList<Goods> TmallList = null;
        ArrayList<Goods> DDList = null;
        ArrayList<Goods> JDList = null;
        ArrayList<Goods> List = null;
        ArrayList<Goods> goodsArrayList = new ArrayList<Goods>();

        if (platform.equals("All")) {
            Future<ArrayList<Goods>> TaoBao = searchTBSNService.SearchTaobao(key);
            Future<ArrayList<Goods>> SuNing = searchTBSNService.SearchSuNing(key);
            Future<ArrayList<Goods>> Tmall = searchOtherService.SearchTmall(key);
            Future<ArrayList<Goods>> DD = searchOtherService.SearchDD(key);
            Future<ArrayList<Goods>> JD = searchOtherService.SearchJD(key);
            TaoBaoList = TaoBao.get();
            SuNingList = SuNing.get();
            TmallList = Tmall.get();
            DDList = DD.get();
            JDList = JD.get();
        }else {
            if (platform.contains("Taobao")) {
                Future<ArrayList<Goods>> TaoBao = searchTBSNService.SearchTaobao(key);
                TaoBaoList = TaoBao.get();
            }
            if (platform.contains("SuNing")) {
                Future<ArrayList<Goods>> SuNing = searchTBSNService.SearchSuNing(key);
                SuNingList = SuNing.get();
            }
            if (platform.contains("Tmall")) {
                Future<ArrayList<Goods>> Tmall = searchOtherService.SearchTmall(key);
                TmallList = Tmall.get();
            }
            if (platform.contains("JD")) {
                Future<ArrayList<Goods>> JD = searchOtherService.SearchJD(key);
                JDList = JD.get();
            }
            if (platform.contains("DD")) {
                Future<ArrayList<Goods>> DD = searchOtherService.SearchDD(key);
                DDList = DD.get();
            }
        }

        if (TaoBaoList != null){
            goodsArrayList.addAll(TaoBaoList);
        }
        if (SuNingList != null){
            goodsArrayList.addAll(SuNingList);
        }
        if (TmallList != null){
            goodsArrayList.addAll(TmallList);
        }
        if (DDList != null) {
            goodsArrayList.addAll(DDList);
        }
        if (JDList != null){
            goodsArrayList.addAll(JDList);
        }

        if (sort == null || sort.equals("top")){
            List = Srot.QuickSort(goodsArrayList, 0, goodsArrayList.size() - 1);
        }else {
            List = Srot.buttomSort(goodsArrayList, 0, goodsArrayList.size() - 1);
        }

        return List;
    }

    @ResponseBody
    @GetMapping("/TaoBao")
    public String TaoBao(@RequestParam("key") String key) throws IOException {
        return searchTBSNService.SearchTaobaoOne(key);
    }

    @ResponseBody
    @GetMapping("/SuNing")
    public String SuNing(@RequestParam("key") String key) throws IOException {
        return searchTBSNService.SearchSuNingOne(key);
    }

    @ResponseBody
    @GetMapping("/Tmall")
    public String Tmall(@RequestParam("key") String key) throws IOException {
        return searchOtherService.SearchTmallOne(key);
    }

    @ResponseBody
    @GetMapping("/DD")
    public String DD(@RequestParam("url") String url) throws IOException {
        return searchOtherService.SearchDDOne(url);
    }

    @ResponseBody
    @GetMapping("/JD")
    public String JD(@RequestParam("key") String key) throws IOException {
        return searchOtherService.SearchJDOne(key);
    }










}
